import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import Taro from '@tarojs/taro'
import appConfig from '@/config'
import {delCamera, selectCameras} from '@/api/ezviz'
import {getStorage} from '@/utils/localstoreage'
import {toEditVideoSurveillance} from '@/utils/pageRedirect'

class Videosurveillance extends ChildrenPageUtils {
  @observable cameraList: Camera.Info[] = []

  @action.bound
  updatePageDataSource() {
    const userId = getStorage('userId')
    if (userId) {
      selectCameras({userId})
        .then(res => {
          this.cameraList = res.data
        })
        .catch(err => {
          this.cameraList = []
        })
    }
  }

  /**跳转到萤石云微信小程序 */
  @action.bound
  redirectToEzvizMiniP(camera: Camera.Info) {
    try {
      Taro.showLoading({title: '打开中...'})

      // 解构参数
      const {accessToken, deviceSerial, channelNo} = camera
      // 判断是否有accessToken
      if (camera.accessToken) {
        let path = appConfig.ezvizConf.liveVideoByMiniProgramPathJoin({
          accessToken,
          deviceSerial,
          channelNo
        })
        Taro.hideLoading()
        Taro.openEmbeddedMiniProgram({
          appId: appConfig.ezvizConf.appId,
          path,
          success(res) {
            // 打开成功
            console.log(res)
          },
          fail(err) {
            console.error(err)
            Taro.showModal({
              content: '打开失败，请重新尝试'
            })
          }
        })
      } else {
        Taro.hideLoading()
        Taro.showModal({
          content: 'accessToken获取失败！'
        })
      }
    } catch (err) {
      Taro.hideLoading()
      console.error(err)
    }
  }

  @observable currentBtnIdx: number | undefined = undefined
  @action.bound
  openBtn(idx: number) {
    if (idx !== this.currentBtnIdx) {
      this.currentBtnIdx = idx
    } else {
      this.closeBtn()
    }
  }
  @action.bound
  closeBtn() {
    this.currentBtnIdx = undefined
  }

  @computed get currentCamera(): Camera.Info | undefined {
    let current = this.cameraList.find(
      (item, idx) => idx === this.currentBtnIdx
    )
    return current
  }
  @action.bound
  toEditCamera() {
    if (this.currentCamera) {
      toEditVideoSurveillance({cameraId: this.currentCamera.cameraId})
    }
  }
  @action.bound
  handleDelCamera() {
    if (this.currentCamera) {
      Taro.showModal({
        content: `确定删除摄像头《${this.currentCamera.cameraName}》吗？`,
        success: result => {
          if (result.confirm && this.currentCamera) {
            delCamera({cameraId: this.currentCamera.cameraId})
              .then(res => {
                if (res.msg) {
                  Taro.showModal({
                    content: res.msg
                  })
                }
                this.updatePageDataSource()
              })
              .catch(err => {
                if (err && err.msg) {
                  Taro.showModal({
                    content: err.msg
                  })
                }
              })
          }
        }
      })
    }
  }
}
export const videoSurveillanceStore = createContext(new Videosurveillance())
